#include <QApplication>
#include <QPushButton>
#include <QTreeView>

#include <iostream>

#include <stdio.h>

#include "MainWindow.h"
#include "TreeModel.h"
#include "Process.h"

#include "proc/readproc.h"

QList<Process *> *processList = new QList<Process *>();
QString pidStr = "";

using namespace std;

void BuildProcessList()
{
  PROCTAB *proct;
  proc_t *proc_info;

  proct = openproc(PROC_FILLARG | PROC_FILLSTAT | PROC_FILLSTATUS);

  int numprocs = 0;
  char *cmd = (char*)malloc(255 * sizeof(char));
  int pid;

  while ((proc_info = readproc(proct,NULL)))
  {
    strncpy(cmd, proc_info->cmd, 255);
    pid = proc_info->tid;
    numprocs++;
    QString *newStr = new QString(cmd);
    Process *newProcess = new Process(pid);
    newProcess->SetTaskName(newStr);

    processList->append(newProcess);
    //QString name = newProcess->GetTaskName();
    pidStr += QString::number(pid) + "\t" + cmd + "\n";

    std::cout << newProcess->GetPid() << "\t\t" << newProcess->GetTaskName()->toStdString() << std::endl;

  }

  closeproc(proct);
  free(cmd);
}

int main(int argc, char *argv[])
{
  BuildProcessList();

  QApplication app(argc, argv);

  //MainWindow mw;
  //mw.show();

  QString str = "Test1\nTest2\nTest3";
  TreeModel model(pidStr);

  QTreeView view;
  view.setModel(&model);
  view.setWindowTitle(QObject::tr("Process List"));
  view.show();
  
  //  QPushButton hello("Hello world!");
  //  hello.resize(100, 30);

  //  hello.show();

  return app.exec();
}
